package com.emm.yixun.estate.service.impl.app.manage;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.mapper.PriceSchemeDetailMapper;
import com.emm.yixun.basic.mapper.PriceSchemeMapper;
import com.emm.yixun.basic.mapper.ProjectRoomMapper;
import com.emm.yixun.basic.service.PriceSchemeDetailService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.common.app.GetRoomListReqDto;
import com.emm.yixun.common.app.GetRoomListResDto;
import com.emm.yixun.common.app.servletentity.RoomListRes;
import com.emm.yixun.common.enums.ResultEnum;
import com.emm.yixun.common.model.PriceScheme;
import com.emm.yixun.common.model.PriceSchemeDetail;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.enums.PropertyTypeEnum;
import com.emm.yixun.common.model.enums.ValuationTypeEnum;

@Service("getRoomListManage")
public class GetRoomListManage {

	@Autowired
	private ProjectRoomService projectRoomService;
	@Autowired
	private ProjectRoomMapper projectRoomMapper;
	@Autowired
	private PriceSchemeDetailService priceSchemeDetailService;
	@Autowired
	private PriceSchemeMapper priceSchemeMapper;
	@Autowired
	private PriceSchemeDetailMapper priceSchemeDetailMapper;
	
	/**
	 * 查询可关注房源-房号
	 * @param reqDto
	 * @return
	 */
	public GetRoomListResDto getRoomList(GetRoomListReqDto reqDto) {
		GetRoomListResDto resDto = new GetRoomListResDto();
		try{
			ProjectRoomDto prDto = new ProjectRoomDto();
			prDto.setMerchantId(reqDto.getMerchantId());
			prDto.setProjectCode(reqDto.getProjectCode());
			if(reqDto.getUnitId() != null && !"".equals(reqDto.getUnitId())){
				prDto.setUnitId(Long.parseLong(reqDto.getUnitId()));
			}
			if(reqDto.getBuildingId() != null && !"".equals(reqDto.getBuildingId())){
				prDto.setBuildingId(Long.parseLong(reqDto.getBuildingId()));
			}
			if(reqDto.getPremisesId() != null && !"".equals(reqDto.getPremisesId())){
				prDto.setPremisesId(Long.parseLong(reqDto.getPremisesId()));
			}
			List<ProjectRoom> prList = projectRoomMapper.selectRoomEntityListByDto(prDto);
			List<RoomListRes> roomList = new ArrayList<RoomListRes>();
			for(ProjectRoom pr: prList){
				//查询公寓最新一条价格方案计价方式类型：筛选对应房源面积
				PriceScheme priceScheme = priceSchemeMapper.selectEntityByRoomId(pr.getId());
				if(null == priceScheme){
					//房源无价格方案,不展示
					continue;
				}
				PriceSchemeDetail priceSchemeDetail = priceSchemeDetailService.selectEntityByRoomId(pr.getId());
				if(null == priceSchemeDetail){
					//房源无价格方案明细,不展示
					continue;
				}
				RoomListRes rlRes = new RoomListRes();
				rlRes.setRoomId(String.valueOf(pr.getId()));
				rlRes.setRoomName(pr.getRoomName());
				//房产类型:[{key:property_type,values:[{no:1,alias:APARTMENT,remark:公寓},{no:2,alias:VILLA,remark:别墅},{no:3,alias:PARKING,remark:车位}]}]
				//价格方案中获取相关数据
				if(PropertyTypeEnum.APARTMENT.getNo().equals(pr.getPropertyType())){//公寓
					if(priceScheme.getValuationType() != null && ValuationTypeEnum.ValuationTypeEnum_2.getNo().equals(priceScheme.getValuationType())){
						rlRes.setCalculateArea(String.valueOf(pr.getInnerArea()));
					}else{
						if(pr.getBuildingArea() != null && !"".equals(pr.getBuildingArea())){
							rlRes.setCalculateArea(String.valueOf(pr.getBuildingArea()));
						}else{
							rlRes.setCalculateArea(null);
						}
					}
					if(priceScheme.getFloorType() != null){
						rlRes.setFloorType(priceScheme.getFloorType());
					}else{
						rlRes.setFloorType(null);
					}
				}else if(PropertyTypeEnum.VILLA.getNo().equals(pr.getPropertyType())){//别墅
					if(pr.getBuildingArea() != null && !"".equals(pr.getBuildingArea())){
						rlRes.setCalculateArea(String.valueOf(pr.getBuildingArea()));
					}else{
						rlRes.setCalculateArea(null);
					}
					if(priceScheme.getFloorType() != null){
						rlRes.setFloorType(priceScheme.getFloorType());
					}else{
						rlRes.setFloorType(null);
					}
				}else if(PropertyTypeEnum.PARKING.getNo().equals(pr.getPropertyType())){//车位无面积
					rlRes.setCalculateArea(null);
				}else{
					//新加一层判断,不符合上述条件.默认根据价格方案相关属性获取面积属性
					if(priceScheme.getValuationType() != null && ValuationTypeEnum.ValuationTypeEnum_2.getNo().equals(priceScheme.getValuationType())){
						rlRes.setCalculateArea(String.valueOf(pr.getInnerArea()));
					}else{
						if(pr.getBuildingArea() != null && !"".equals(pr.getBuildingArea())){
							rlRes.setCalculateArea(String.valueOf(pr.getBuildingArea()));
						}else{
							rlRes.setCalculateArea(null);
						}
					}
					if(priceScheme.getFloorType() != null){
						rlRes.setFloorType(priceScheme.getFloorType());
					}else{
						rlRes.setFloorType(null);
					}
				}
				//方案明细中获取相关数据
				if(priceSchemeDetail.getUnitPrice() != null && !"".equals(priceSchemeDetail.getUnitPrice())){
					rlRes.setDiscountPrice(String.valueOf(priceSchemeDetail.getUnitPrice()));
				}else{
					rlRes.setDiscountPrice(null);
				}
				if(priceSchemeDetail.getTotalPrice() != null && !"".equals(priceSchemeDetail.getTotalPrice())){
					/*Double totalPrice = Double.valueOf(String.valueOf(priceSchemeDetail.getTotalPrice()))/10000;*/
					rlRes.setDiscountTotalPrice(String.valueOf(priceSchemeDetail.getTotalPrice()));
				}else{
					rlRes.setDiscountTotalPrice(null);
				}
				
				if(null != priceScheme && null != priceScheme.getFloorType() && "1".equals(priceScheme.getFloorType())){
					//折扣方案,需要根据折扣比例计算
					BigDecimal t = new BigDecimal(1);
					BigDecimal t2 = new BigDecimal(100);
					//折扣比例 = 1 - 折扣比例/100
					BigDecimal discountRate = priceSchemeDetail.getDiscountRate().divide(t2);
					BigDecimal  zkbl = t.subtract(discountRate);//折扣比例
					BigDecimal zkjg = priceSchemeDetail.getTotalPrice().multiply(zkbl);//折后价格 = 总价*折扣比例
					PriceSchemeDetail priceSchemeDetail2 = priceSchemeDetailMapper.selectSchemeEntityByRoomId(pr.getId());
					rlRes.setDiscountAmount(String.valueOf(zkjg));
					rlRes.setDiscountRate(String.valueOf(priceSchemeDetail2.getDiscountRate().divide(t2)));
					//初始化进来,无折扣金额
					//rlRes.setDiscountAmount("0");
					//rlRes.setDiscountRate(String.valueOf(t2.divide(t2)));
				}else{
					if(priceSchemeDetail.getDiscountRate() == null){
						rlRes.setDiscountAmount(null);
						rlRes.setDiscountRate(null);
					}else{
						rlRes.setDiscountAmount(String.valueOf(priceSchemeDetail.getTotalPrice().subtract(priceSchemeDetail.getDiscountRate())));
						rlRes.setDiscountRate(String.valueOf(priceSchemeDetail.getDiscountRate()));
					}
				}
				/**
				 * 折扣类型: 0低价,1折扣,逻辑错误
				 * JHSong_2016-10-21_操作: 注释
				BigDecimal t = new BigDecimal(1);
				BigDecimal t2 = new BigDecimal(100);
				if(priceSchemeDetail.getDiscountRate() != null && !"".equals(priceSchemeDetail.getDiscountRate())){
					BigDecimal discountRate = priceSchemeDetail.getDiscountRate().divide(t2);
					BigDecimal  zkbl = t.subtract(discountRate);
					BigDecimal zkjg = priceSchemeDetail.getTotalPrice().multiply(zkbl);
					if(null != priceScheme && null != priceScheme.getFloorType() && "1".equals(priceScheme.getFloorType())){
						PriceSchemeDetail priceSchemeDetail2 = priceSchemeDetailMapper.selectSchemeEntityByRoomId(pr.getId());
						rlRes.setDiscountRate(String.valueOf(priceSchemeDetail2.getDiscountRate().divide(t2)));
					}else{
						rlRes.setDiscountAmount(String.valueOf(zkjg));
						rlRes.setDiscountRate(String.valueOf(discountRate));
					}
				}else{
					rlRes.setDiscountAmount(null);
					rlRes.setDiscountRate(null);
				}
				*/
				roomList.add(rlRes);
			}
			resDto.setRoomList(roomList);
			resDto.setResult(ResultEnum.SUCCESS.getNo());
		}catch(Exception e){
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("可关注房号查询失败!");
			resDto.setResult("0");
		}
		return resDto;
	}
	
}
